var express = require('express');
var router = express.Router();
var connect = require('../public/javascripts/connect');

var results = {};
router.get('/', function (req, res, next) {
    let sql = 'SELECT * FROM gudongtoupiao ORDER BY 序号';
    connect.connection3.query(sql, function (err, result, fields) {
        if (err) {
            console.log('[SELECT ERROR] - ', err.message);
            return;
        }
        results.name = result;
        console.log("dataBar", results);
        res.render('jilutable.jade', results);
    })
});

router.post('/', function (req, res, next) {
    let {num, yian, val} = req.body;
    console.log(num, yian, val);
    let yian2 = yian.split('-')[1];
    let sql = `UPDATE gudongtoupiao SET 第${yian2}个议案=${val} WHERE 序号=${num}`;
    connect.connection3.query(sql, function (err, result, fields) {
        if (err) {
            console.log('[SELECT ERROR] - ', err.message);
            return;
        }
        console.log("relust", result);
    })
    res.json({"success": 1});
})

router.post('/clear', function (req, res, next) {
    let {xuhao} = req.body;
    console.log(xuhao)
    let sql = `UPDATE gudongtoupiao SET 第1个议案=3,第2个议案=3,第3个议案=3,第4个议案=3,第5个议案=3,第6个议案=3,第7个议案=3,第8个议案=3 WHERE 序号 = ${xuhao}`;
    connect.connection3.query(sql, function (err, result, fields) {
        if (err) {
            console.log('[SELECT ERROR] - ', err.message);
            return
        }
        console.log("relust", result);
    })
    res.json({"success": 1});
})

module.exports = router;
